/*
 * Created on 2004-2-6
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.page;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;


/**
 * @author vikings
 *
 */
public class PagingTag extends TagSupport{

	private static final long serialVersionUID = 1L;

	private int pageSize = 5;
	private int currPage=1;
	private int itemCount = 0;
	private String href = null;

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getItemCount() {
		return itemCount;
	}

	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public int doStartTag() throws JspException
	{
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException
	{


		int totalPages = 0;
		totalPages = itemCount / pageSize;
		//如果总记录数为0 或者总页数为0，不输出分页控制标签（页面看不到上一页下页首末页的输出），返回EVAL_PAGE，实际值为6，
		if(itemCount==0||pageSize==0){
			return EVAL_PAGE;
		}
		if (itemCount % pageSize != 0) {
			totalPages = totalPages + 1;
		}
		if (totalPages<=1)
		{
			return EVAL_PAGE;
		}
		
		StringBuffer navigation = new StringBuffer();
		navigation.append("<ul class='pageUl'>");
		navigation.append("<li><a>第"+currPage+"页/共"+totalPages+"页</a></li>");
		if (currPage<=1){//如果当前页为第一页，所以没有首页和上一页的点击效果
			navigation.append("<li><a>首页</a></li>");
			navigation.append("<li><a>上一页</a></li>");
		} else {
			navigation.append("<li><a href='"+href+"?currPage=1&pageSize="+pageSize+"'>首页</a></li>");
			navigation.append("&nbsp;");
			navigation.append("<li><a href='"+href+"?currPage="+(currPage-1)+"&pageSize="+pageSize+"'>上一页</a></li>");
		}
		navigation.append("&nbsp;");
		if (currPage>=totalPages){
			navigation.append("<li><a>下一页</a></li>");
			navigation.append("&nbsp;");
			navigation.append("<li><a>尾页</a></li>");
		}else {
			navigation.append("<li><a href='"+href+"?currPage="+(currPage+1)+"&pageSize="+pageSize+"'>下一页</a></li>");
			navigation.append("&nbsp;");
			navigation.append("<li><a href='"+href+"?currPage="+((itemCount + pageSize - 1)/pageSize)+"&pageSize="+pageSize+"'>尾页&nbsp;</a></li>");
		}
		navigation.append("</ul>");
		try {
			pageContext.getOut().print(navigation.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public void release()
	{
		super.release();
		this.pageSize = 0; //recordsPerPage
		this.currPage=1; //beginItem
		this.itemCount = 0;
		this.href = null;
	}
}
